<?php

!defined('IN') && exit('Access denied');

function cartAddProduct($pid) {
    global $_G;
    if (!$_G['uid']) return true;

    $pid = (int)$pid;

    $product_name = $_ENV['db']->fetchCol('SELECT name FROM product where product_id = '.$pid);
    if ($product_name) {
        $sql = "SELECT count(1) FROM cart WHERE uid = '{$_G['uid']}' AND product_id = '{$pid}'";
        $count = $_ENV['db']->fetchCol($sql);

        if ($count > 0) {
            $sql = "UPDATE `cart` SET num=num+1 WHERE uid = '{$_G['uid']}' and product_id = '{$pid}'";
        } else {
            $sql = "INSERT INTO cart SET uid='{$_G['uid']}', product_id='{$pid}' , num = 1, product_name = '{$product_name}'";
        }

        $_ENV['db']->runSql($sql);
    }
}